In [1]:
# -*- coding: utf-8 -*-
# !/usr/bin/python
# Cross Wavelet Analysis (CWA) based on Maraun and Kurths(2004).
# http://www.nonlin-processes-geophys.net/11/505/2004/npg-11-505-2004.pdf
# author: Mabel Calim Costa
# INPE
# 23/01/2013
# reviewed --- 02/03/2018
"""
Created on Mon Jun 17 2013

@author: Mabel Calim Costa
"""
import numpy as np
import pylab
from pylab import *
import matplotlib.pyplot as plt
import cmath
import pandas as pd

Now, let's create an artificial signal to test it!

  • create a time series X (sine)
  • create a time series Y (cosine) = out of phase sine

In [2]:
# using the example : example_python3.6.ipynb
import numpy as np
from pylab import *
import waipy
z = np.linspace(0,2048,2048)
x = np.sin(50*np.pi*z)

data_norm_x = waipy.normalize(x)
result_x = waipy.cwt(data_norm_x, 1, 1, 0.125, 2, 4/0.125, 0.72, 6,mother='Morlet',name='x')
waipy.wavelet_plot('x', z, x, 0.03125, result_x)


1024 1024
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

In [3]:
#let's create another artificial signal to compare with that!
#y = np.cos(50*np.pi*z)  # signal out of phase from x
noise = np.sin(7*np.pi*z/3)+np.cos(2*np.pi*z/3)
y = np.sin(30*np.pi*z/3)+noise #out of phase signal compared to x + noise
data_norm_y = waipy.normalize(y)
result_y = waipy.cwt(data_norm_y, 1, 1, 0.125, 2, 4/0.125, 0.72, 6,mother='Morlet',name='x')
waipy.wavelet_plot('y', z, y, 0.03125, result_y)


1024 1024
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

Let's check the cross power wavelet spectrum!

  • note that arrows indicate in phase when pointing to the right and out of phase when pointing left.

In [8]:
cross_power, coherence, phase_angle = cross_wavelet(result_x['wave'],result_y['wave'])
plot_cross('signals', cross_power, phase_angle, z, result_x, result_y)
#Arrows indicate in phase when pointing to the right and out of phase when pointing left.


/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/ma/core.py:2778: ComplexWarning: Casting complex values to real discards the imaginary part
  order=order, subok=True, ndmin=ndmin)

Now let's view the wavelet coherence analysis

for X and Y signals


In [9]:
cross_power, coherence, phase_angle = cross_wavelet(result_x['wave'],result_y['wave'])


plot_cohere('signals',coherence,z,result_x, result_y)


Just check if coherence analysis is OK!

-- try to execute the same example with the same signal, e.g. X with X. Coherence should be exactly 1.


In [305]:
cross_power, coherence, phase_angle = cross_wavelet(result_x['wave'],result_x['wave'])


plot_cohere('signals',coherence,z,result_x, result_x)



In [ ]: